{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import networkx as nx\n",
    "\n",
    "pseudo_count = 0.01"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1. 生成全局图、并记录原始图权重"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [],
   "source": [
    "global_graph = nx.DiGraph()\n",
    "with open(r'./data/test-net/global_graph.txt', 'r', encoding='utf-8') as f:\n",
    "    for line in f:\n",
    "        line = line.strip()\n",
    "        parts = line.split(\"\\t\\t\")\n",
    "        source = int(parts[0])\n",
    "        global_graph.add_node(source)\n",
    "        if parts[1] != \"null\":\n",
    "            node_freq_strs = parts[1].split(\"\\t\")\n",
    "            # print(len(node_freq_strs))\n",
    "            for node_freq_str in node_freq_strs:\n",
    "                node_freq = node_freq_str.split(\":\")\n",
    "                weight = int(node_freq[1])\n",
    "                target = int(node_freq[0])\n",
    "                global_graph.add_node(target)\n",
    "                global_graph.add_edge(source, target, global_weight=weight)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2. 读取级联数据，对每一个级联数据生成级联图，并进行随机游走"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "def parse_graph(graph_string: str) -> nx.DiGraph:\n",
    "    graph_string = graph_string.strip()\n",
    "    parts = graph_string.split('\\t')\n",
    "    edge_strs = parts[4].split()\n",
    "    res_graph = nx.DiGraph()\n",
    "    for edge_str in edge_strs:\n",
    "        source, target, _ = edge_str.split(':')\n",
    "        source, target = int(source), int(target)\n",
    "        res_graph.add_node(source)\n",
    "        res_graph.add_node(target)\n",
    "        res_graph.add_edge(source, target,\n",
    "                           edge_weight=global_graph.edges[(source, target)]['global_weight'] + pseudo_count,\n",
    "                           global_degree=global_graph.out_degree[target])\n",
    "    for edge in res_graph.edges:\n",
    "        res_graph.add_edge(edge[0], edge[1], res_graph.out_degree[edge[1]])\n",
    "    return res_graph\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "97550",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_22760/660376231.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m     33\u001B[0m                 \u001B[0mres_graph\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0madd_node\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtarget\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     34\u001B[0m                 res_graph.add_edge(source, target,\n\u001B[1;32m---> 35\u001B[1;33m                                    \u001B[0medge_weight\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mglobal_graph\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0medges\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msource\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mtarget\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'global_weight'\u001B[0m\u001B[1;33m]\u001B[0m \u001B[1;33m+\u001B[0m \u001B[0mpseudo_count\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     36\u001B[0m                                    global_degree=global_graph.out_degree[target])\n\u001B[0;32m     37\u001B[0m             \u001B[1;32mfor\u001B[0m \u001B[0medge\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mres_graph\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0medges\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Environment\\Miniconda3\\lib\\site-packages\\networkx\\classes\\reportviews.py\u001B[0m in \u001B[0;36m__getitem__\u001B[1;34m(self, e)\u001B[0m\n\u001B[0;32m   1030\u001B[0m             )\n\u001B[0;32m   1031\u001B[0m         \u001B[0mu\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mv\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0me\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1032\u001B[1;33m         \u001B[1;32mreturn\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_adjdict\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mu\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mv\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1033\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1034\u001B[0m     \u001B[1;31m# EdgeDataView methods\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mKeyError\u001B[0m: 97550"
     ]
    }
   ],
   "source": [
    "def parse_graph(graph_string: str) -> nx.DiGraph:\n",
    "    graph_string = graph_string.strip()\n",
    "    parts = graph_string.split('\\t')\n",
    "    edge_strs = parts[4].split()\n",
    "    res_graph = nx.DiGraph()\n",
    "    for edge_str in edge_strs:\n",
    "        source, target, _ = edge_str.split(':')\n",
    "        source, target = int(source), int(target)\n",
    "        res_graph.add_node(source)\n",
    "        res_graph.add_node(target)\n",
    "        res_graph.add_edge(source, target,\n",
    "                           edge_weight=global_graph.edges[(source, target)]['global_weight'] + pseudo_count,\n",
    "                           global_degree=global_graph.out_degree[target])\n",
    "    for edge in res_graph.edges:\n",
    "        res_graph.add_edge(edge[0], edge[1], res_graph.out_degree[edge[1]])\n",
    "    return res_graph\n",
    "\n",
    "read_cas_file = './data/test-net/cascade_test.txt'\n",
    "write_cas_file = './data/test-net/random_walks_train.txt'\n",
    "with open(read_cas_file, 'r', encoding='utf-8') as rf:\n",
    "    with open(write_cas_file, 'w', encoding='utf-8') as wf:\n",
    "        for line in rf:\n",
    "            # cas_graph = parse_graph(line)\n",
    "\n",
    "            graph_string = line.strip()\n",
    "            parts = graph_string.split('\\t')\n",
    "            edge_strs = parts[4].split()\n",
    "            res_graph = nx.DiGraph()\n",
    "            for edge_str in edge_strs:\n",
    "                source, target, _ = edge_str.split(':')\n",
    "                source, target = int(source), int(target)\n",
    "                res_graph.add_node(source)\n",
    "                res_graph.add_node(target)\n",
    "                res_graph.add_edge(source, target,\n",
    "                                   edge_weight=global_graph.edges[(source, target)]['global_weight'] + pseudo_count,\n",
    "                                   global_degree=global_graph.out_degree[target])\n",
    "            for edge in res_graph.edges:\n",
    "                res_graph.add_edge(edge[0], edge[1], res_graph.out_degree[edge[1]])\n",
    "            # random_str = random_walk(line)\n",
    "            # wf.write(random_str + '\\n')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5de9a36ef058094593c623284e9c36786895fc54f6b4a6d58564e65103a25547"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}